package beer.xz.beer.device.ui.selection

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import beer.xz.beer.device.R
import beer.xz.beer.device.data.product.Products

@Composable
fun SelectionScreen(onCancel: () -> Unit) {
    Box(
        modifier = Modifier
            .size(1024.dp, 600.dp)
            .background(color = MaterialTheme.colorScheme.background)
            .padding(bottom = 20.dp),
        contentAlignment = Alignment.Center
    ) {
        val simpleProductData = Products.getProduct()
        val selected = remember { mutableIntStateOf(0) }
        Column(horizontalAlignment = Alignment.CenterHorizontally) {
            Image(
                painter = painterResource(id = R.drawable.bgselection),
                contentDescription = "Product Image",
//                modifier = Modifier.size(200.dp)
                modifier = Modifier
                    .size(480.dp, 720.dp)
                    .shadow(
                        elevation = 20.dp, // 阴影的高度
                        ambientColor = Color.Black.copy(alpha = 0.5f), // 环境光颜色
                        spotColor = Color.Black.copy(alpha = 1f) // 聚光颜色
                    )
            )
            Spacer(modifier = Modifier.size(40.dp))
            Row(
                verticalAlignment = Alignment.CenterVertically
            ) {
                Text(
                    text = "选择容量:",
                    modifier = Modifier.width(200.dp),
                    fontSize = 24.sp,
                    fontWeight = FontWeight.Bold,
                    color = Color.Red
                )
                simpleProductData.prices.sortedBy { it.capacity }.forEach { specPrice ->
                    Text(
                        text = specPrice.specName,

                        modifier = Modifier
                            .clickable { selected.intValue = specPrice.capacity }
                            .width(200.dp),
//                            .border(
//                                if (selected.intValue == specPrice.capacity) {
//                                    BorderStroke(2.dp, Color.Blue)
//                                } else {
//                                    BorderStroke(2.dp, Color.Transparent)
//                                }
//                            ),
                        fontSize = 24.sp,
                        fontWeight = if (selected.intValue == specPrice.capacity) FontWeight.Bold else FontWeight.Normal,
                        color = if (selected.intValue == specPrice.capacity) Color.Blue else Color.Black,
                    )
//                    Spacer(modifier = Modifier.size(40.dp))
                }
//                Text(text = "This is the selection screen!")
            }


            Spacer(modifier = Modifier.size(40.dp))

            Row() {
                Button(
                    {},
                    modifier = Modifier.size(200.dp, 60.dp),
                    colors = ButtonDefaults.buttonColors(
                        contentColor = Color.Red, containerColor = Color.Green
                    )
                ) {
                    Text(text = "开始", fontSize = 30.sp)
                }
                Spacer(modifier = Modifier.size(50.dp))
                Button({ onCancel() }, modifier = Modifier.size(200.dp, 60.dp)) {
                    Text(text = "取消", fontSize = 30.sp)
                }
            }
        }

    }
}


@Preview
@Composable
fun test() {
    SelectionScreen({})
}